package com.example.demo.servlet;

/**
 * 负责流程控制 和 页面跳转 main.jsp页面
 */

import com.example.demo.bean.Student;
import com.example.demo.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "MainServlet",urlPatterns = "/m-servlet")
public class MainServlet extends HttpServlet {
    private StudentService studentService;

    public MainServlet() {
        this.studentService = new StudentService();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        设置从浏览器获得的数据的字符集，不然出现乱码
        req.setCharacterEncoding("utf-8");
//        获取请求的处理类型
        String type = (String) req.getAttribute("type") == null ? req.getParameter("type") : (String) req.getAttribute("type");
        List<Student> students = null;
        Cookie[] cookies = req.getCookies();
        System.out.println(type);
        //刷新和添加请求处理，返回更新的数据
        if(type.equals("ref")) {
            students = refresh(cookies, resp);
        }
        //翻页请求处理，返回指定页数的数据，后台分页
        if(type.equals("pre")||type.equals("next")) {
            int temp = type.equals("pre") ? -14 : 14;
            for (Cookie c : cookies) {
                if(c.getName().equals("pageStart")) {
                    int pageStart = Integer.parseInt(c.getValue());
                    if(pageStart < 14&&type.equals("pre")) {
                        students = studentService.getAllStudentService(0, 14);
                    }else if(studentService.getStudentCountService() - pageStart <= 14&&type.equals("next")) {
                        students = studentService.getAllStudentService(pageStart, 14);
                    }else{
                        students = studentService.getAllStudentService(pageStart + temp, 14);
                        c.setValue("" + (pageStart + temp));
                        resp.addCookie(c);
                    }
                }
            }
        }
        //添加请求处理 ，返回添加结果res
        if(type.equals("add")) {
            Long res = studentService.addStudentService(new Student(0, req.getParameter("formSid"), req.getParameter("formName"),
                    req.getParameter("formGender"), req.getParameter("formBirthday"), req.getParameter("formEmail"),
                    req.getParameter("formImp")));
            students = refresh(cookies, resp);
            Cookie cookie = new Cookie("res","" + res);
            resp.addCookie(cookie);
        }
        //修改学生信息，并返回新的第一页
        if(type.equals("set")) {
            studentService.setStudentService(new Student(0, req.getParameter("formSid"), req.getParameter("formName"),
                    req.getParameter("formGender"), req.getParameter("formBirthday"), req.getParameter("formEmail"),
                    req.getParameter("formImp")));
            students = refresh(cookies,resp);
        }
        //删除数据返回新的第一页
        if(type.equals("del")) {
            String parameter = req.getParameter("sids");
            String[] sids = parameter.split(",");
            studentService.delStudentService(sids);
            students = refresh(cookies,resp);
        }
        //根据要求的id或者名字查询并返回符合条件的学生信息
        if(type.equals("que")) {
            String queSid = req.getParameter("queSid");
            String queSname = req.getParameter("queSname");
            System.out.println(queSid + " " + queSname);
            students = studentService.queStudentService(queSid,queSname);
            System.out.println(students);
        }
        req.setAttribute("students",students);
        req.getRequestDispatcher("main.jsp").forward(req,resp);
    }

    //用于刷新页面，返回第一页的数据
    private List<Student> refresh(Cookie[] cookies, HttpServletResponse resp) {
        List<Student> students = new StudentService().getAllStudentService(0, 14);
        for (Cookie c: cookies) {
            if(c.getName().equals("pageStart")) {
                c.setValue("" + 0);
                resp.addCookie(c);
            }
        }
        return students;
    }
}
